package com.woniuxy.mapper;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.woniuxy.pojo.Grade;

@CacheNamespace // 开启二级缓存
public interface GradeMapper {

    @Select("select * from grade")
    public List<Grade> all();

    @Insert("INSERT INTO grade VALUES (DEFAULT,#{grade_name},#{grade_time},#{user_id})")
    public int add(Grade grade);

	// 分页查询
	@Select("SELECT * FROM grade LIMIT #{0},#{1}")
	@Results({ @Result(id = true, column = "grade_id", property = "grade_id"),
			@Result(column = "grade_name", property = "grade_name"),
			@Result(column = "grade_time", property = "grade_time"), @Result(column = "user_id", property = "user_id"),
			@Result(column = "user_id", property = "teacherName", one = @One(

					select = "findNameByUid",

					fetchType = FetchType.LAZY

			))

	})
	public List<Grade> ShowAllByPage(int pageNumber, int pageSize);

	// 查询班级数量
	@Select("select count(*) from grade")
	public int GradeCount();

	// 通过user_id查name
	@Select("select user_name from user where user_number = #{user_id}")
	public String findNameByUid(int user_id);

	// 根据班级id查班级
	@Select("SELECT * FROM grade WHERE grade_id = #{id}")
	public Grade findGradeById(int grade_id);

	// 通过id统计该班级的人数
	@Select("SELECT COUNT(*) FROM student WHERE grade_id = #{id}")
	public int findCountById(int grade_id);

	// 删除班级
	@Delete("DELETE FROM grade WHERE grade_id = #{id}")
	public int delete(int grade_id);
}
